<?php
namespace Util;
use think\Db;

class Tool{

    public function xxx(){
        echo 123;
    }

    public function curl($url,$data=false,$time=10,$header=false,$user_agent=false){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);//设置抓取的url
        $user_agent&&curl_setopt($ch, CURLOPT_USERAGENT,$user_agent);
        $data&&curl_setopt($ch, CURLOPT_POST, true);//设置post方式提交
        $data&&curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置post数据
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $header&&curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置获取的信息以字符流的形式返回，而不是直接输出
        curl_setopt($ch,CURLOPT_HEADER,false);//是否输出页面内容
        curl_setopt($ch, CURLOPT_TIMEOUT, $time);//抓取页面的等待时间为30秒
        //curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
        $response = curl_exec($ch);//执行命令
        if ($response === FALSE){
            echo 'cURL Error:'.curl_error($ch);
        }
        curl_close($ch);//关闭URL请求
        //处理返回数据
        //var_dump($response);
        return $response;
    }


    public function Tips($code=400000,$msg='参数错误',$data=false){
        exit(json_encode(array('code'=>$code,'msg'=>$msg,'data'=>$data),JSON_UNESCAPED_UNICODE));
    }

    //接收上传图片
    public function upload_file($folder='test',$name='',$type='img'){
        if ($_FILES["file"]["error"] > 0){
            echo "错误：" . $_FILES["file"]["error"] . "<br>";
            echo "上传文件名: " . $_FILES["file"]["name"] . "<br>";
            echo "文件类型: " . $_FILES["file"]["type"] . "<br>";
            echo "文件大小: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
            echo "文件临时存储的位置: " . $_FILES["file"]["tmp_name"];
            die;
        }
        //dump(DOCUMENT_ROOT.'/public/upload');die;

        $file = request()->file('file');
        if($file==null){
            exit(json_encode(array('code'=>1,'msg'=>'没有文件上传')));
        }
        $info = $file->getInfo();
        //dump($info);
        if($info['size']>20480000){
            exit(json_encode(array('code'=>1,'msg'=>'文件不能大于20MB')));
        }
        if($type=='img'&&!in_array($info['type'],array('image/jpg','image/jpeg','image/gif','image/png'))){
            exit(json_encode(array('code'=>1,'msg'=>'文件格式不支持')));
        }
        $file_name = '.'.$folder;
        //echo $file_name;
        if(!is_dir($file_name)){
            mkdir($file_name,0777,true);
        }
        if(!$name){
            $name =date('YmdHms',time()).rand(0,1000);
        }
        $info = $file->move($file_name,$name);
        $img = $folder.'/'.$info->getSaveName();
        exit(json_encode(array('code'=>0,'msg'=>$img)));
    }

        //写日志
    public function writeLog($content,$filename='Create')
    {
        $path = DOCUMENT_ROOT.'/Log/'.$filename;
        if (!is_dir($path)){
            mkdir($path,0777,true);
        }
        $file = $path.'/'.$filename.'_'.date('Y_m_d').'.txt';
        file_put_contents($file,$content.PHP_EOL,FILE_APPEND);  // 最简单的快速的以追加的方式写入写入方法，
    }

}